﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dack_Web2
{
    public partial class SanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (Request.QueryString["MaLoai"] != null)
            {
                int Trang = 1;
                if (Request.QueryString["Page"] != null)
                {
                    Trang = int.Parse(Request.QueryString["Page"].ToString());
                }
                int sodong = int.Parse(ConfigurationManager.AppSettings["records"].ToString());
                string MSP = Request.QueryString["MaLoai"].ToString();
                string sql = string.Format("select d.NgayKetThuc,sp.*, (GiaSanPham - (GiaSanPham / 100) * Giam) as DaGiam from Deal d, SanPham sp where d.MaSanPham = sp.MaSanPham and d.NgayBatDau <= @NgayHienTai and d.NgayKetThuc >= @NgayHienTai and MaLoaiSanPham = {0} and d.XacNhan = @XacNhan and MaDeal between {1} and {2}", MSP, (Trang - 1) * sodong - 1, (Trang - 1) * sodong + sodong);
                OleDbCommand cmd = new OleDbCommand(sql);
                OleDbParameter[] pr = {new OleDbParameter("@NgayHienTai", DateTime.Now.Date),
                                        new OleDbParameter("@XacNhan",true)};
                DtSanPham.DataSource = Sql_DataProvider.SQL_ExecuteTable(cmd,pr);
                DtSanPham.DataBind();
                Phan_Trang(Trang,MSP);
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        private void Phan_Trang(int Curent, string MSP)
        {
            string sql = string.Format("select Count(*) from [Deal] d where d.NgayBatDau <= @NgayHienTai and d.NgayKetThuc >= @NgayHienTai");
            OleDbCommand cmd = new OleDbCommand(sql);
            OleDbParameter pr = new OleDbParameter("@NgayHienTai", DateTime.Now.Date);           
            cmd.Parameters.Add(pr);
            int TongBai = int.Parse(Sql_DataProvider.SQL_ExecuteScalar(cmd).ToString());
            int Trang = TongBai / int.Parse(ConfigurationManager.AppSettings["records"].ToString()) + 1;
            HyperLink HyperLinks = new HyperLink();
            HyperLinks.Text = "<< Trang Đầu ";
            HyperLinks.NavigateUrl = "~/SanPham.aspx?MaLoai=" + MSP + "&Page=1";
            if (Curent == 1)
            {
                HyperLinks.Font.Bold = true;
            }
            Panel1.Controls.Add(HyperLinks);
            for (int i = 1; i <= Trang; i++)
            {
                HyperLink HyperLinkk = new HyperLink();
                if (i == Curent)
                {
                    HyperLinkk.Font.Bold = true;
                }
                HyperLinkk.Text = " " + i.ToString() + " ";
                HyperLinkk.NavigateUrl = "~/SanPham.aspx?MaLoai=" + MSP + "&Page=" + i.ToString();
                HyperLinkk.Visible = true;
                HyperLinkk.Attributes.Add("ID", "Hyp" + i.ToString());
                this.Panel1.Controls.Add(HyperLinkk);
            }
            HyperLinks = new HyperLink();
            HyperLinks.Text = " Trang cuối >>";
            HyperLinks.NavigateUrl = "~/SanPham.aspx?MaLoai=" + MSP + "&Page=" + Trang;
            if (Curent == Trang)
            {
                HyperLinks.Font.Bold = true;
            }
            Panel1.Controls.Add(HyperLinks);
        }
    }
}